package com.tanhua.dubbo.api;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tanhua.dubbo.mappers.LogMapper;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.domain.Log;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.AgeRangeEnum;
import com.tanhua.model.enums.AreaEnum;
import com.tanhua.model.enums.SexEnum;
import com.tanhua.model.vo.DistributionVo;
import com.tanhua.model.vo.LogVo;
import com.tanhua.model.vo.ReturnVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

@DubboService
public class UserApiImpl  implements UserApi{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    //根据手机号码查询用户
    public User findByMobile(String mobile) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("mobile",mobile);
        return userMapper.selectOne(qw);
    }

    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public void updatePhone(User user) {
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(user.getMobile()!=null,User::getMobile,user.getMobile());
        User user1 = userMapper.selectOne(lqw);
        //判断user1是否为空
        if(user1!=null){
            //不为空表示这个手机号已绑定其他账号
            throw new RuntimeException("这个手机号已绑定其他账号");
        }
        //user1为空,表示这个手机号可以换绑
        userMapper.updateById(user);
    }

    @Override
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectList(Wrappers.emptyWrapper());
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public User findByHuanxin(String huanxinId) {
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(huanxinId!=null,User::getHxUser,huanxinId);
        return userMapper.selectOne(lqw);
    }
    @Override
    public Integer userCount() {
        Integer count = userMapper.selectCount(null);
        return count;
    }

    @Override
    public Integer newUserCount(Integer day) {
        List<User> users = userMapper.selectList(null);
        List<Date> dates = CollUtil.getFieldValues(users, "created", Date.class);
        Integer count = 0;
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        if (day == -1) {
            for (Date date : dates) {
                if (date.getTime() <= calendar.getTimeInMillis()) {
                    calendar.add(calendar.DATE, -1);
                    if (date.getTime() >= calendar.getTimeInMillis()) {
                        count++;
                    }
                }
            }
            return count;
        } else {
            for (Date date : dates) {
                if (date.getTime() >= calendar.getTimeInMillis()) {
                    count++;
                }
            }
        }

        return count;
    }

    @Override
    public Integer finActivityForDays(Integer day) {
        LambdaQueryWrapper<Log> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.groupBy(Log::getUserId);
        List<Log> logs = logMapper.selectList(queryWrapper);
        Integer count = 0;
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        if (day == -1) {
            for (Log log : logs) {
                try {
                    Date oldTime = sdf.parse(log.getLogTime());
                    if (oldTime.getTime() <= calendar.getTimeInMillis()) {
                        calendar.add(calendar.DATE,-1);
                        if (oldTime.getTime() >= calendar.getTimeInMillis()) {
                            count++;
                        }
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            return count;
        } else if (day == -2) {
            for (Log log : logs) {
                try {
                    Date oldTime = sdf.parse(log.getLogTime());
                    calendar.add(calendar.DATE,-1);
                    if (oldTime.getTime() <= calendar.getTimeInMillis()) {
                        calendar.add(calendar.DATE,-1);
                        if (oldTime.getTime() >= calendar.getTimeInMillis()) {
                            count++;
                        }
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            return count;
        } else {
            calendar.add(calendar.DATE,-day);
            for (Log log : logs) {
                try {
                    Date oldTime = sdf.parse(log.getLogTime());
                    if (oldTime.getTime() >= calendar.getTimeInMillis()) {
                        count++;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            return count;
        }
    }

    @Override
    public Long getUseTimePassWeek() {
        LocalDate today = LocalDate.now();
        LocalDate week = today.plusDays(-7);
        QueryWrapper<Log> qw = new QueryWrapper<>();
        qw.apply("date_format (created,'%Y-%m-%d') >= date_format('" + week + "','%Y-%m-%d')")
                .apply("date_format (created,'%Y-%m-%d') <= date_format('" + today + "','%Y-%m-%d')");
        List<Log> infos = logMapper.selectList(qw);//所有信息

        long time = 0L;
        int count = 0;
        for (int i = 0; i < infos.size(); i++) {
            QueryWrapper<Log> wrapper = new QueryWrapper<>();
            wrapper.select("created").eq("user_id", infos.get(i).getUserId()).like("created", infos.get(i).getLogTime()).groupBy("created").orderByAsc("created");
            List<Log> list = logMapper.selectList(wrapper);
            if (list.size() > 1) {
                long timeNew = list.get(1).getCreated().getTime();
                long timeOld = list.get(0).getCreated().getTime();
                long l = timeNew - timeOld;
                time += Math.abs(l);
                count++;
            }
        }
        if (time!=0&&count!=0) {
            return (time/count/7)+57600000;
        }
        return 1086820l;
    }

    @Override
    public HashMap newUserCount(LocalDate start, LocalDate end) {

        List<ReturnVo> thisYear = getnewUserVos(start, end);
        List<ReturnVo> lastYear = getnewUserVos(start.minusYears(1), end.minusYears(1));

        HashMap ret = new HashMap();
        ret.put("thisYear", thisYear);
        ret.put("lastYear", lastYear);
        return ret;
    }
    @Override
    public HashMap activeUserCount(LocalDate start, LocalDate end) {

        List<LogVo> thisYear = getActiveVos(start, end);
        List<LogVo> lastYear = getActiveVos(start.minusYears(1), end.minusYears(1));

        HashMap ret = new HashMap();
        ret.put("thisYear", thisYear);
        ret.put("lastYear", lastYear);
        return ret;
    }

    @Override
    public HashMap retainedUserCount(LocalDate start, LocalDate end) {

        List<LogVo> thisYear = getRetainedVos(start, end);
        List<LogVo> lastYear = getRetainedVos(start.minusYears(1), end.minusYears(1));

        HashMap ret = new HashMap();
        ret.put("thisYear", thisYear);
        ret.put("lastYear", lastYear);
        return ret;
    }


    private List<ReturnVo> getnewUserVos(LocalDate start, LocalDate end) {

        int differenceDay = (int) start.until(end, ChronoUnit.DAYS);
        int differenceMonth = (int) start.until(end, ChronoUnit.MONTHS);
        int differenceYear = (int) start.until(end, ChronoUnit.YEARS);

        List<ReturnVo> thisYear = new ArrayList<>();

        if (differenceMonth <= 2) {//日
            for (int i = 0; i <= differenceDay; i++) {
                int year = start.plusDays(i).getYear();//获得年
                int month = start.plusDays(i).getMonthValue();//获得月
                int day = start.plusDays(i).getDayOfMonth();//获得日
                thisYear.add(new ReturnVo("" + month + "月" + day + "日", userMapper.selectCount(new QueryWrapper<User>()
                        .select("DISTINCT id")
                        .apply("date_format (created,'%Y-%m-%d') = date_format('" + year + "-" + month + "-" + day + "','%Y-%m-%d')")))
                );
            }
        }

        if (differenceMonth >= 3 && differenceMonth <= 12) {//月
            for (int i = 0; i <= differenceMonth; i++) {
                int year = start.plusMonths(i).getYear();//获得年
                int month = start.plusMonths(i).getMonthValue();//获得月

                if (i == 0) {
                    thisYear.add(new ReturnVo("" + year + "年" + month + "月", userMapper.selectCount(new QueryWrapper<User>()
                            .select("DISTINCT id")
                            .apply("date_format (created,'%Y-%m-%d') >= date_format('" + year + "-" + month + "-" + start.getDayOfMonth() + "','%Y-%m-%d')")
                            .apply("date_format (created,'%Y-%m-%d') <= date_format('" + year + "-" + month + "-31','%Y-%m-%d')")))
                    );
                    continue;
                }
                if (i == differenceMonth) {
                    thisYear.add(new ReturnVo("" + year + "年" + month + "月", userMapper.selectCount(new QueryWrapper<User>()
                            .select("DISTINCT id")
                            .apply("date_format (created,'%Y-%m-%d') >= date_format('" + year + "-" + month + "-1','%Y-%m-%d')")
                            .apply("date_format (created,'%Y-%m-%d') <= date_format('" + year + "-" + month + "-" + end.getDayOfMonth() + "','%Y-%m-%d')")))

                    );
                    continue;
                }
                thisYear.add(new ReturnVo("" + year + "年" + month + "月", userMapper.selectCount(new QueryWrapper<User>()
                        .select("DISTINCT id")
                        .apply("date_format (created,'%Y-%m-%d') >= date_format('" + year + "-" + month + "-1','%Y-%m-%d')")
                        .apply("date_format (created,'%Y-%m-%d') <= date_format('" + year + "-" + month + "-31','%Y-%m-%d')")))
                );
            }
        }

        if (differenceMonth >= 13) {//年

            if (LocalDate.now().getYear() != end.getYear()) {
                return thisYear;
            }

            for (int i = 0; i <= differenceYear; i++) {
                int year = start.plusYears(i).getYear();//获得年

                if (i == 0) {
                    thisYear.add(new ReturnVo("" + year + "年", userMapper.selectCount(new QueryWrapper<User>()
                            .select("DISTINCT id")
                            .apply("date_format (created,'%Y-%m-%d') >= date_format('" + year + "-" + start.getMonthValue() + "-" + start.getDayOfMonth() + "','%Y-%m-%d')")
                            .apply("date_format (created,'%Y-%m-%d') <= date_format('" + year + "-12-31','%Y-%m-%d')")))
                    );
                    continue;
                }

                if (i == differenceYear) {
                    thisYear.add(new ReturnVo("" + year + "年", userMapper.selectCount(new QueryWrapper<User>()
                            .select("DISTINCT id")
                            .apply("date_format (created,'%Y-%m-%d') >= date_format('" + year + "-1-1','%Y-%m-%d')")
                            .apply("date_format (created,'%Y-%m-%d') <= date_format('" + year + "-" + end.getMonthValue() + "-" + end.getDayOfMonth() + "','%Y-%m-%d')")))
                    );
                    continue;
                }

                thisYear.add(new ReturnVo("" + year + "年", userMapper.selectCount(new QueryWrapper<User>()
                        .select("DISTINCT id")
                        .apply("date_format (created,'%Y-%m-%d') >= date_format('" + year + "-1-1','%Y-%m-%d')")
                        .apply("date_format (created,'%Y-%m-%d') <= date_format('" + year + "-12-31','%Y-%m-%d')")))
                );
            }
        }
        return thisYear;
    }


    private List<LogVo> getActiveVos(LocalDate start, LocalDate end) {

        int differenceDay = (int) start.until(end, ChronoUnit.DAYS);
        int differenceMonth = (int) start.until(end, ChronoUnit.MONTHS);
        int differenceYear = (int) start.until(end, ChronoUnit.YEARS);

        List<LogVo> thisYear = new ArrayList<>();

        if (differenceMonth <= 2) {//日
            for (int i = 0; i <= differenceDay; i++) {
                int year = start.plusDays(i).getYear();//获得年
                int month = start.plusDays(i).getMonthValue();//获得月
                int day = start.plusDays(i).getDayOfMonth();//获得日
                thisYear.add(new LogVo("" + month + "月" + day + "日", logMapper.selectCount(new QueryWrapper<Log>()
                        .select("DISTINCT user_id")
                        .apply("date_format (log_time,'%Y-%m-%d') = date_format('" + year + "-" + month + "-" + day + "','%Y-%m-%d')")))
                );
            }
        }

        if (differenceMonth >= 3 && differenceMonth <= 12) {//月
            for (int i = 0; i <= differenceMonth; i++) {
                int year = start.plusMonths(i).getYear();//获得年
                int month = start.plusMonths(i).getMonthValue();//获得月

                if (i == 0) {
                    thisYear.add(new LogVo("" + year + "年" + month + "月", logMapper.selectCount(new QueryWrapper<Log>()
                            .select("DISTINCT user_id")
                            .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-" + month + "-" + start.getDayOfMonth() + "','%Y-%m-%d')")
                            .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-" + month + "-31','%Y-%m-%d')")))
                    );
                    continue;
                }
                if (i == differenceMonth) {
                    thisYear.add(new LogVo("" + year + "年" + month + "月", logMapper.selectCount(new QueryWrapper<Log>()
                            .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-" + month + "-1','%Y-%m-%d')")
                            .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-" + month + "-" + end.getDayOfMonth() + "','%Y-%m-%d')")))

                    );
                    continue;
                }
                thisYear.add(new LogVo("" + year + "年" + month + "月", logMapper.selectCount(new QueryWrapper<Log>()
                        .select("DISTINCT user_id")
                        .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-" + month + "-1','%Y-%m-%d')")
                        .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-" + month + "-31','%Y-%m-%d')")))
                );
            }
        }

        if (differenceMonth >= 13) {//年

            if (LocalDate.now().getYear() != end.getYear()) {
                return thisYear;
            }

            for (int i = 0; i <= differenceYear; i++) {
                int year = start.plusYears(i).getYear();//获得年

                if (i == 0) {
                    thisYear.add(new LogVo("" + year + "年", logMapper.selectCount(new QueryWrapper<Log>()
                            .select("DISTINCT user_id")
                            .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-" + start.getMonthValue() + "-" + start.getDayOfMonth() + "','%Y-%m-%d')")
                            .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-12-31','%Y-%m-%d')")))
                    );
                    continue;
                }

                if (i == differenceYear) {
                    thisYear.add(new LogVo("" + year + "年", logMapper.selectCount(new QueryWrapper<Log>()
                            .select("DISTINCT user_id")
                            .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-1-1','%Y-%m-%d')")
                            .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-" + end.getMonthValue() + "-" + end.getDayOfMonth() + "','%Y-%m-%d')")))
                    );
                    continue;
                }

                thisYear.add(new LogVo("" + year + "年", logMapper.selectCount(new QueryWrapper<Log>()
                        .select("DISTINCT user_id")
                        .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-1-1','%Y-%m-%d')")
                        .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-12-31','%Y-%m-%d')")))
                );
            }
        }
        return thisYear;
    }


    private List<LogVo> getRetainedVos(LocalDate start, LocalDate end) {

        int differenceDay = (int) start.until(end, ChronoUnit.DAYS);
        int differenceMonth = (int) start.until(end, ChronoUnit.MONTHS);
        int differenceYear = (int) start.until(end, ChronoUnit.YEARS);

        List<LogVo> thisYear = new ArrayList<>();

        if (differenceMonth <= 2) {//日
            for (int i = 0; i <= differenceDay; i++) {
                int year = start.plusDays(i).getYear();//获得年
                int month = start.plusDays(i).getMonthValue();//获得月
                int day = start.plusDays(i).getDayOfMonth();//获得日

                List<Long> ids = logMapper.selectList(new QueryWrapper<Log>()
                        .select("user_id")
                        .apply("date_format (log_time,'%Y-%m-%d') = date_format('" + year + "-" + month + "-" + day + "','%Y-%m-%d')")
                        .groupBy("user_id")
                        .orderByAsc("user_id")).stream().map(Log::getUserId).collect(Collectors.toList());

                int count = 0;
                for (Long id : ids) {
                    List<String> times = logMapper.selectList(new QueryWrapper<Log>()
                            .select("log_time").eq("user_id", id)
                            .groupBy("log_time").orderByAsc("log_time")).stream().map(Log::getLogTime).collect(Collectors.toList());
                    if (times.size() > 1 && LocalDate.parse(times.get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1).equals(LocalDate.parse(times.get(1), DateTimeFormatter.ofPattern("yyyy-MM-dd")))) {
                        count++;
                    }
                }

                thisYear.add(new LogVo("" + month + "月" + day + "日", count));
            }
        }

        if (differenceMonth >= 3 && differenceMonth <= 12) {//月

            for (int i = 0; i <= differenceMonth; i++) {
                int year = start.plusMonths(i).getYear();//获得年
                int month = start.plusMonths(i).getMonthValue();//获得月

                if (i == 0) {
                    List<Long> ids = logMapper.selectList(new QueryWrapper<Log>()
                            .select("user_id")
                            .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-" + month + "-" + start.getDayOfMonth() + "','%Y-%m-%d')")
                            .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-" + month + "-31','%Y-%m-%d')")
                            .groupBy("user_id")
                            .orderByAsc("user_id")).stream().map(Log::getUserId).collect(Collectors.toList());

                    int count = 0;
                    for (Long id : ids) {
                        List<String> times = logMapper.selectList(new QueryWrapper<Log>()
                                .select("log_time").eq("user_id", id)
                                .groupBy("log_time").orderByAsc("log_time")).stream().map(Log::getLogTime).collect(Collectors.toList());
                        if (times.size() > 1 && LocalDate.parse(times.get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1).equals(LocalDate.parse(times.get(1), DateTimeFormatter.ofPattern("yyyy-MM-dd")))) {
                            count++;
                        }
                    }

                    thisYear.add(new LogVo("" + year + "年" + month + "月", count));
                    continue;
                }


                if (i == differenceMonth) {
                    List<Long> ids = logMapper.selectList(new QueryWrapper<Log>()
                            .select("user_id")
                            .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-" + month + "-1','%Y-%m-%d')")
                            .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-" + month + "-" + end.getDayOfMonth() + "','%Y-%m-%d')")
                            .groupBy("user_id")
                            .orderByAsc("user_id")).stream().map(Log::getUserId).collect(Collectors.toList());

                    int count = 0;
                    for (Long id : ids) {
                        List<String> times = logMapper.selectList(new QueryWrapper<Log>()
                                .select("log_time").eq("user_id", id)
                                .groupBy("log_time").orderByAsc("log_time")).stream().map(Log::getLogTime).collect(Collectors.toList());
                        if (times.size() > 1 && LocalDate.parse(times.get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1).equals(LocalDate.parse(times.get(1), DateTimeFormatter.ofPattern("yyyy-MM-dd")))) {
                            count++;
                        }
                    }

                    thisYear.add(new LogVo("" + year + "年" + month + "月", count));
                    continue;
                }

                List<Long> ids = logMapper.selectList(new QueryWrapper<Log>()
                        .select("user_id")
                        .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-" + month + "-1','%Y-%m-%d')")
                        .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-" + month + "-31','%Y-%m-%d')")
                        .groupBy("user_id")
                        .orderByAsc("user_id")).stream().map(Log::getUserId).collect(Collectors.toList());

                int count = 0;
                for (Long id : ids) {
                    List<String> times = logMapper.selectList(new QueryWrapper<Log>()
                            .select("log_time").eq("user_id", id)
                            .groupBy("log_time").orderByAsc("log_time")).stream().map(Log::getLogTime).collect(Collectors.toList());
                    if (times.size() > 1 && LocalDate.parse(times.get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1).equals(LocalDate.parse(times.get(1), DateTimeFormatter.ofPattern("yyyy-MM-dd")))) {
                        count++;
                    }
                }

                thisYear.add(new LogVo("" + year + "年" + month + "月", count));
            }
        }


        if (differenceMonth >= 13) {//年

            if (LocalDate.now().getYear() != end.getYear()) {
                return thisYear;
            }

            for (int i = 0; i <= differenceYear; i++) {
                int year = start.plusYears(i).getYear();//获得年

                if (i == 0) {

                    List<Long> ids = logMapper.selectList(new QueryWrapper<Log>()
                            .select("user_id")
                            .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-" + start.getMonthValue() + "-" + start.getDayOfMonth() + "','%Y-%m-%d')")
                            .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-12-31','%Y-%m-%d')")
                            .groupBy("user_id")
                            .orderByAsc("user_id")).stream().map(Log::getUserId).collect(Collectors.toList());

                    int count = 0;
                    for (Long id : ids) {
                        List<String> times = logMapper.selectList(new QueryWrapper<Log>()
                                .select("log_time").eq("user_id", id)
                                .groupBy("log_time").orderByAsc("log_time")).stream().map(Log::getLogTime).collect(Collectors.toList());
                        if (times.size() > 1 && LocalDate.parse(times.get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1).equals(LocalDate.parse(times.get(1), DateTimeFormatter.ofPattern("yyyy-MM-dd")))) {
                            count++;
                        }
                    }

                    thisYear.add(new LogVo("" + year + "年", count));
                    continue;
                }

                if (i == differenceYear) {

                    List<Long> ids = logMapper.selectList(new QueryWrapper<Log>()
                            .select("user_id")
                            .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-1-1','%Y-%m-%d')")
                            .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-" + end.getMonthValue() + "-" + end.getDayOfMonth() + "','%Y-%m-%d')")
                            .groupBy("user_id")
                            .orderByAsc("user_id")).stream().map(Log::getUserId).collect(Collectors.toList());

                    int count = 0;
                    for (Long id : ids) {
                        List<String> times = logMapper.selectList(new QueryWrapper<Log>()
                                .select("log_time").eq("user_id", id)
                                .groupBy("log_time").orderByAsc("log_time")).stream().map(Log::getLogTime).collect(Collectors.toList());
                        if (times.size() > 1 && LocalDate.parse(times.get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1).equals(LocalDate.parse(times.get(1), DateTimeFormatter.ofPattern("yyyy-MM-dd")))) {
                            count++;
                        }
                    }

                    thisYear.add(new LogVo("" + year + "年", count));
                    continue;
                }

                List<Long> ids = logMapper.selectList(new QueryWrapper<Log>()
                        .select("user_id")
                        .apply("date_format (log_time,'%Y-%m-%d') >= date_format('" + year + "-1-1','%Y-%m-%d')")
                        .apply("date_format (log_time,'%Y-%m-%d') <= date_format('" + year + "-12-31','%Y-%m-%d')")
                        .groupBy("user_id")
                        .orderByAsc("user_id")).stream().map(Log::getUserId).collect(Collectors.toList());

                int count = 0;
                for (Long id : ids) {
                    List<String> times = logMapper.selectList(new QueryWrapper<Log>()
                            .select("log_time").eq("user_id", id)
                            .groupBy("log_time").orderByAsc("log_time")).stream().map(Log::getLogTime).collect(Collectors.toList());
                    if (times.size() > 1 && LocalDate.parse(times.get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1).equals(LocalDate.parse(times.get(1), DateTimeFormatter.ofPattern("yyyy-MM-dd")))) {
                        count++;
                    }
                }

                thisYear.add(new LogVo("" + year + "年", count));

            }
        }
        return thisYear;
    }


    @Override
    public List<DistributionVo> getAgeRange(LocalDate start, LocalDate end) {
        QueryWrapper<UserInfo> qw = new QueryWrapper<>();
        qw.apply("date_format (created,'%Y-%m-%d') >= date_format('" + start + "','%Y-%m-%d')")
                .apply("date_format (created,'%Y-%m-%d') <= date_format('" + end + "','%Y-%m-%d')");
        List<UserInfo> infos = userInfoMapper.selectList(qw);//所有用户信息

        qw.select("age");
        List<Integer> list = userInfoMapper.selectList(qw).stream().map(UserInfo::getAge).collect(Collectors.toList());
        int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0;
        for (Integer age : list) {
            if (age < AgeRangeEnum.UNDER_TWENTY.getMax()) {
                c1++;
            } else if (age < AgeRangeEnum.TWENTY.getMax()) {
                c2++;
            } else if (age < AgeRangeEnum.THIRTY.getMax()) {
                c3++;
            } else if (age < AgeRangeEnum.FORTY.getMax()) {
                c4++;
            } else {
                c5++;
            }
        }
        List<DistributionVo> collect = new ArrayList<>();
        collect.add(new DistributionVo(AgeRangeEnum.UNDER_TWENTY.getDesc(), c1));
        collect.add(new DistributionVo(AgeRangeEnum.TWENTY.getDesc(), c2));
        collect.add(new DistributionVo(AgeRangeEnum.THIRTY.getDesc(), c3));
        collect.add(new DistributionVo(AgeRangeEnum.FORTY.getDesc(), c4));
        collect.add(new DistributionVo(AgeRangeEnum.OVER_FIFTY.getDesc(), c5));

        return collect;
    }


    @Override
    public List<DistributionVo> getGenderRange(LocalDate start, LocalDate end) {
        QueryWrapper<UserInfo> qw = new QueryWrapper<>();
        qw.apply("date_format (created,'%Y-%m-%d') >= date_format('" + start + "','%Y-%m-%d')")
                .apply("date_format (created,'%Y-%m-%d') <= date_format('" + end + "','%Y-%m-%d')");
        List<UserInfo> infos = userInfoMapper.selectList(qw);//所有用户信息

        qw.select("gender");
        List<String> list = userInfoMapper.selectList(qw).stream().map(UserInfo::getGender).collect(Collectors.toList());
        int c1 = 0, c2 = 0;
        for (String gender : list) {
            if (gender.equals(SexEnum.WOMAN.toString())) {
                c1++;
            } else {
                c2++;
            }
        }
        List<DistributionVo> collect = new ArrayList<>();
        collect.add(new DistributionVo(SexEnum.WOMAN.toString(), c1));
        collect.add(new DistributionVo(SexEnum.MAN.toString(), c2));

        return collect;
    }

    @Override
    public List<DistributionVo> getLocalRange(LocalDate start, LocalDate end) {
        List<String> list = getLocation(start, end);
        if (list == null) {
            return null;
        }
        List<DistributionVo> collect = new ArrayList<>();

        l:
        for (String location : list) {
            if (collect.size() == 0) {
                collect.add(new DistributionVo(location, 1));
            }
            for (DistributionVo vo : collect) {
                if (vo.getTitle().equals(location)) {
                    vo.setAmount(vo.getAmount() + 1);
                    continue l;
                }
            }
            collect.add(new DistributionVo(location, 1));
        }

        return collect;
    }


    @Override
    public List<DistributionVo> getLocalTotal(LocalDate start, LocalDate end) {
        List<String> list = getLocation(start, end);
        if (list == null) {
            return null;
        }
        List<DistributionVo> collect = new ArrayList<>();

        List<String> areas = list.stream().map(AreaEnum::getAreaByProvince).collect(Collectors.toList());

        l:
        for (String area : areas) {
            if (collect.size() == 0) {
                collect.add(new DistributionVo(area, 1));
            }
            for (DistributionVo vo : collect) {
                if (vo.getTitle().equals(area)) {
                    vo.setAmount(vo.getAmount() + 1);
                    continue l;
                }
            }
            collect.add(new DistributionVo(area, 1));
        }

        return collect;
    }

    @Override
    public List<DistributionVo> getIndustry(LocalDate start, LocalDate end) {
        QueryWrapper<UserInfo> qw = new QueryWrapper<>();
        qw.apply("date_format (created,'%Y-%m-%d') >= date_format('" + start + "','%Y-%m-%d')")
                .apply("date_format (created,'%Y-%m-%d') <= date_format('" + end + "','%Y-%m-%d')");
        List<UserInfo> infos = userInfoMapper.selectList(qw);//所有用户信息

        if (infos == null) {
            return null;
        }

        qw.select("profession").groupBy("profession");

        return userInfoMapper.selectList(qw).stream().map((item) -> {
            Integer count = 0;
            for (int i = infos.size() - 1; i >= 0; i--) {
                if (infos.get(i).getProfession().equals(item.getProfession())) {
                    count++;
                }
            }
            return new DistributionVo(item.getProfession(), count);
        }).collect(Collectors.toList());
    }

    private List<String> getLocation(LocalDate start, LocalDate end) {
        QueryWrapper<UserInfo> qw = new QueryWrapper<>();
        qw.apply("date_format (created,'%Y-%m-%d') >= date_format('" + start + "','%Y-%m-%d')")
                .apply("date_format (created,'%Y-%m-%d') <= date_format('" + end + "','%Y-%m-%d')");
        List<UserInfo> infos = userInfoMapper.selectList(qw);//所有用户信息

        if (infos == null) {
            return null;
        }

        qw.select("city");
        List<String> temp = userInfoMapper.selectList(qw).stream().map(UserInfo::getCity).collect(Collectors.toList());
        return temp.stream().map((item) -> item.split("-")[0].split("市")[0].split("省")[0]).collect(Collectors.toList());
    }

}
